{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. Quantum mechanics\n",
    "\n",
    "\n",
    "Quantum mechanics is a theory which describes physical phenomena. The ideas behind quantum mechanics often appear counter intuitive and in conflict with our everyday experience of the world but they are particularly useful for understanding the behavior of particles at small scales, such as atoms and molecules. In fact, it was at these rather small scales that the breakdown of the classical laws of physics was first encountered. Nonetheless, the realm of quantum mechanics is not confined to the atomic world but it extends almost to all known scales (some problems arise in the interior of black holes and other exotic places for example). When dealing with objects of macroscopic size, the predictions of quantum mechanics agree with the one which can be made using classical laws of physics such as Newtonian mechanics and Maxwell's equations of the electromagnetic field. \n",
    "\n",
    "In the previous chapter, we encountered almost all the mathematical machinery needed to do calculations in quantum mechanics. In fact, in quantum mechanics physical systems and the interaction between them can be described using the matrix representation of the Hilbert space formalism. Here, we will make a connection between the mathematical concepts introduced and their relation to the physical world. The mathematics works extremely well, it has been tested countless times over the years without ever being unsuccessful in describing experiments. What has turned out to be very difficult is to associate an interpretation to the meaning of these mathematical abstractions. We will give here the standard and most widespread interpretation of the connection between the mathematical framework of quantum mechanics to our physical reality.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.1 Postulates of quantum mechanics\n",
    "\n",
    "Here, we present quantum mechanics as a tool for a new paradigm of computing. Thus, we will introduce it in an axiomatic way and quickly put it to use. First, we introduce the axioms of the theory, then we describe the connection to the physical interpretation of these abstract concepts. \n",
    "Let us briefly go over the axioms, or postulates, of quantum mechanics. These make up the mathematical foundation of the theory.\n",
    "\n",
    "<ol>\n",
    "        <li> All the possible states of a system form an Hilbert space.</li>\n",
    "        <li> The wave function $\\lvert \\psi \\rangle$, which is a ket vector in the Hilbert space, encodes the state of the system. Complex linear combinations of the states of the system are still valid states of the system. </li>\n",
    "        <li> Physical properties of a system, called observables, are encoded into the real eigenvalues of Hermitian operators on a Hilbert space.</li>\n",
    "        <li> The evolution of the system in time is generated by a particular operator, the Hamiltonian. The Hamiltonian is the operator associated with the total energy of the system. </li>\n",
    "        <li> The expected value of a physical property $\\hat{F}$ of the system in the state $\\lvert \\psi \\rangle $ is found by calculating $ \\langle \\psi \\rvert \\hat{F} \\lvert \\psi \\rangle$.</li>\n",
    "    </ol>\n",
    "\n",
    "Using the rules given above one can make accurate predictions about properties of interest in any given setting.\n",
    "\n",
    "#### Example\n",
    "For example, consider a two-level system, a qubit, let us see how the postulates are used in this simple case.\n",
    "The possible state of the qubit are either high/low (on/off, 1/0, up/down,..), so the Hilbert space is just made up by these two vectors $\\lvert 0 \\rangle$, $\\lvert 1 \\rangle$. One usually says that these form a \"basis\" for the Hilbert space, as any vector in the space can be found as a complex linear combination of these two. \n",
    "\n",
    "The state of the qubit is represented by a vector in this Hilbert space. The most general vector is $\\lvert \\psi \\rangle = \\alpha \\lvert 0 \\rangle + \\beta \\lvert 1 \\rangle $.\n",
    "\n",
    "Consider the Hamiltonian $\\hat{H} = E_0 \\hat{\\sigma}^{+}\\hat{\\sigma}^{-}$ for the system, where $\\hat{\\sigma}^{+} \\lvert 0 \\rangle = \\lvert 1 \\rangle $, $\\hat{\\sigma}^{+} \\lvert 1 \\rangle = 0 $ and $\\hat{\\sigma}^{-} \\lvert 1 \\rangle = \\lvert 0 \\rangle $, $\\hat{\\sigma}^{-} \\lvert 0 \\rangle = 0 $.\n",
    "\n",
    "The Hamiltonian encodes the energy of the system. Therefore, we can calculate the energy of the qubit in the state $\\lvert 0 \\rangle$: $E = \\langle 0 \\rvert \\hat{H} \\lvert 0 \\rangle = 0$.\n",
    "On the other hand, if the qubit is in the \"on\" state $\\lvert 1 \\rangle$, its energy is: $E = \\langle 1 \\rvert \\hat{H} \\lvert 1 \\rangle = E_0$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.2 The wave function\n",
    "\n",
    "The state of a system is described by a ket vector in Hilbert space, called wave function $\\vert \\psi\\left( \\vec{r} , t \\right)\\rangle $, where the vector $\\vec{r}$ specifies the position at time $t$. The wave function has the following properties:\n",
    "   <ol>\n",
    "        <li> It is a complex function of $\\vec{r}$ and $t$. </li>\n",
    "        <li> It is a continuous function.</li>\n",
    "        <li> Generally its derivative is also continuous function.</li>\n",
    "        <li> Its modulo square, $\\left\\vert \\psi \\right\\vert ^{2}$, is integrable and has physical meaning as the probability of observing the system in the state represented by $\\lvert \\psi \\rangle$.</li>\n",
    "    </ol>\n",
    "    \n",
    "The wave function, being a ket vector, inherits the superposition principle. Therefore, it is possible to build any wave function as a weighted combination of other wave functions. \n",
    "   <ol>\n",
    "        <li> If the system can be both in the state $\\vert \\psi_1 \\rangle $ and in state $\\vert \\psi_2 \\rangle $, then the total state of the system can be written as: $\\vert \\psi \\rangle = \\alpha_1 \\lvert \\psi_1 \\rangle + \\alpha_2 \\lvert \\psi_2 \\rangle $,        \n",
    "where $\\alpha_1$, $\\alpha_2$ are complex numbers whose modulo square represent the probability of finding the system in the corresponding states. </li>\n",
    "        <li> If the wave function is multiplied by any number $\\alpha \\neq 0$, the state described by the corresponding wave function will not change: $\\vert \\psi \\rangle \\leftrightarrow \\alpha \\lvert \\psi \\rangle$ </li>\n",
    "   </ol>\n",
    "    \n",
    "The fact that the superposition principle applies to the states of a system does not have any analog in classical mechanics. A similar idea appears in the description of wave phenomena but it is difficult to understand it in terms of physical states. This is where the intuition we have about the physical world starts failing us. For instance, if one interprets $\\vert \\psi_1 \\rangle $ to mean that the system is located at site $1$ and $\\vert \\psi_2 \\rangle$ that the system is at site $2$, then the superposition state $\\vert \\psi \\rangle = \\alpha_1 \\lvert \\psi_1 \\rangle + \\alpha_2 \\lvert \\psi_2 \\rangle $ would mean that the system is located at two different sites at the same time. The role of the observation, or measurement, comes here into play to save us from the weirdness of the quantum world. Even though the system might be described as being in a superposition of several states, the system's properties won't be an average between the properties of the different states of the superposition. Whenever a measurement is performed, the system \"chooses\" one of the possible states in a non-deterministic way. Which means that when the system is measured, it will be found to be in a particular state of the superposition with a certain probability. This probability is given by the modulo square of the complex coefficient of that state. Therefore, when the system is actually measured, it will be found with probability $\\lvert \\alpha_1 \\rvert^2$ at site $1$ and with probability $\\lvert \\alpha_2 \\rvert^2$ at site $2$. To summarize, at any given time a system may be in a superposition of several different states. However, when a measurement is done on the system, the system will show the properties of one of the states of the superposition with a certain probability. \n",
    "    \n",
    "A consequence of the superposition principle, is that the wave function, $\\vert \\psi \\rangle$, must be the solution of a linear equation. Thus, the equations describing the connection between the state of the system at one time and the state of the system at another time must be linear equations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.3 The operators\n",
    "\n",
    "To each physical quantity $F$ corresponds an operator $\\hat{F}$. Operators specify a mathematical prescription that is carried out on the state. In the matrix representation, we say that operators \"act\" on states, meaning that we have to multiply the vector which represent the state by the matrix which represents the operator. The outcome is a new vector, which represents a new state of the system. The operation of finding the value of a quantity $F$ corresponding to a certain operator $\\hat{F}$ for the system in a state $\\vert \\psi \\rangle$ is denoted by the symbol $\\langle F \\rangle $ which is a short-hand notation for\n",
    "\n",
    "\n",
    "\n",
    "\\begin{equation}\n",
    " \\langle \\hat{F} \\rangle \\equiv \\langle \\psi \\vert  \\hat{F} \\vert \\psi  \\rangle .\n",
    "\\end{equation}\n",
    "\n",
    "To obtain the expected value of the property $F$ for the system in the state $\\lvert \\psi \\rangle$, one needs to decompose the state $\\lvert \\psi \\rangle$ in terms of the eigenstates $\\lvert \\phi_i \\rangle$ of the operator $\\hat{F}$. Those are the vectors that satisfy: \n",
    "\n",
    "\\begin{equation}\n",
    "\\hat{F} \\lvert \\phi_i \\rangle = F_i \\lvert \\phi_i \\rangle .\n",
    "\\end{equation}\n",
    "\n",
    "The decomposition of $\\lvert \\psi \\rangle$ in terms of the $\\lvert \\phi_i \\rangle$ then reads \n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi \\rangle = \\sum_i a_i \\lvert \\phi_i \\rangle,\n",
    "\\end{equation}\n",
    "\n",
    "\n",
    "where $a_i = \\langle \\phi_i \\vert \\psi \\rangle$. Once we have found $\\lvert \\psi \\rangle = \\sum_i a_i \\lvert \\phi_i \\rangle$, we can then find the expected value of $\\hat{F}$ for the system as\n",
    "\n",
    "\\begin{equation}\n",
    " \\langle \\hat{F} \\rangle = \\sum_i \\vert a_i \\vert^2 F_i .\n",
    "\\end{equation}\n",
    "\n",
    "\n",
    "\n",
    "Operators must have the following properties:\n",
    "\n",
    "<ol>\n",
    "        <li> The principle of superposition must be satisfied\n",
    "        \t\t$\\hat{F} \\alpha_1 \\lvert \\psi_1 \\rangle + \\alpha_2 \\lvert \\psi_2 \\rangle = \\alpha_1 \\hat{F} \\lvert \\psi_1 \\rangle + \\alpha_2 \\hat{F} \\lvert \\psi_2 \\rangle $.\n",
    "\t\t\n",
    "\t\tThus, we require the operators to be linear. </li>\n",
    "        <li> The value of quantities represented by operators must be real\n",
    "        \n",
    "        \t\t$ \\langle \\hat{F} \\rangle = \\langle \\hat{F} \\rangle^{ *}$,\n",
    "\t\t\n",
    "\t\twhich means\n",
    "\t\t\n",
    "\t\t$ \\langle \\psi \\vert  \\hat{F} \\vert \\psi  \\rangle  = \\left( \\langle \\psi \\vert  \\hat{F} \\vert \\psi  \\rangle \\right)^{*} = \\langle \\psi \\vert  \\hat{F}^{\\dagger} \\vert \\psi  \\rangle  $,\n",
    "\t\t\n",
    "\t\tTherefore,\n",
    "\t\t\n",
    "\t\t$  \\hat{F} = \\hat{F}^{\\dagger} $.\n",
    "\t\n",
    "\t\tOperators are Hermitian. </li>\n",
    "</ol>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.4 The time evolution\n",
    "\n",
    "The evolution in time of the state $\\vert \\psi\\left( \\vec{r} , t \\right)\\rangle$ of the system is determined by its Hamiltonian operator. One way to mathematically describe the time evolution of the state of the system is with the Schrodinger equation\n",
    "\n",
    "\\begin{equation}\n",
    " -i \\hbar \\frac{\\partial\\vert \\psi\\left( \\vec{r} , t \\right)\\rangle}{\\partial t} = \\hat{H} \\vert \\psi  \\rangle .\n",
    "\\end{equation}\n",
    "\n",
    "The equation above can be solved by specifying the Hamiltonian $\\hat{H}$ operator of the system and the initial state of the system $\\vert \\psi\\left( \\vec{r} , t = 0 \\right)\\rangle$. The Hamiltonian operator is the operator associated with the total energy of the system i.e. it is given by the sum of its potential and kinetic energy.\n",
    "\n",
    "The Schrodinger equation is one of the most important equations of modern physics. It provides an accurate description of all quantum systems which move at non-relativistic speed. In particular, a quantum computer is built by using the Schrodinger equation to understand the behavior of each of its quantum components.\n",
    "\n",
    "However, in quantum computing the picture is simpler and there is no need to use the Schrodinger equation to find the time-evolution of the system. The same happens in a classical computer, where Maxwell's equations are not needed to describe it but the rules of Boolean's logic for the electrical circuits suffice. So, in a quantum computation time evolves in steps where at each step some quantum gate is applied to the qubits. Therefore, the state of the qubits at some time $t$, after $k$ steps of the computation, is completely specified by the quantum circuit which is used in the computation.\n",
    "\n",
    "We can reconcile the two descriptions by introducing the \"time-evolution operator\" $\\hat{U}$, which is defined from his action on the state of the system at $t=0$:\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi \\left( \\vec{r} , t \\right)\\rangle = \\hat{U} \\vert \\psi\\left( \\vec{r} , t=0 \\right)\\rangle\n",
    "\\end{equation}\n",
    "\n",
    "In general, $\\hat{U}$ can be obtained by integrating the Schrodinger equation. For the case of a time-independent Hamiltonian one finds $\\hat{U}=e^{i\\hat{H}t/\\hbar}$\n",
    "\n",
    "In quantum computing, $\\hat{U}$ will be the product of all successive unitary operations encoded in the quantum gates of a circuit. Therefore, the time-evolution operator at time $t_k$, after $k$ steps of the computation, is $\\hat{U}(t=t_k) \\equiv \\hat{U}_k $ and the state of the system evolves as\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi\\left( t_k \\right)\\rangle = \\hat{U}_k \\vert \\psi \\left(  t_0 \\right)\\rangle\n",
    "\\end{equation}\n",
    "\n",
    "\n",
    "#### Example\n",
    "\n",
    "Consider the following quantum circuit\n",
    "\n",
    "<img src=\"figures/3/example1.jpeg\"  width=\"300\">\n",
    "$$\\text{1. Quantum circuit.}$$\n",
    "\n",
    "Let's calculate the time-evolution operators at each time step in order to give the evolution of the state in time\n",
    "$$\\hat{U}_1 = \\hat{I} $$\n",
    "$$\\hat{U}_2 = \\hat{X} \\hat{I} $$\n",
    "$$\\hat{U}_3 = \\hat{X} \\hat{X} \\hat{I}  $$\n",
    "\n",
    "where $\\hat{X}$ is the quantum NOT gate which flips the state of a qubit $ \\lvert 0 \\rangle \\rightarrow  \\lvert 1 \\rangle$, $ \\lvert 1 \\rangle \\rightarrow  \\lvert 0 \\rangle$ \n",
    "We initialize the system to the $\\lvert 0 \\rangle$ at the beginning of the computation, thus at time $t_0$ we have\n",
    "$$\\lvert \\psi \\left( t_0 \\right)\\rangle = \\lvert 0 \\rangle$$\n",
    "\n",
    "At following times we find\n",
    "$$\\lvert \\psi \\left( t_1 \\right)\\rangle = \\hat{U}_1 \\lvert 0 \\rangle = \\hat{I} \\lvert 0 \\rangle = \\lvert 0 \\rangle $$\n",
    "$$\\lvert \\psi \\left( t_2 \\right)\\rangle = \\hat{U}_2 \\lvert 0 \\rangle = \\hat{X} \\hat{I} \\lvert 0 \\rangle = \\hat{X} \\lvert 0 \\rangle = \\lvert 1 \\rangle  $$\n",
    "$$\\lvert \\psi \\left( t_3 \\right)\\rangle = \\hat{U}_3 \\lvert 0 \\rangle = \\hat{X} \\hat{X} \\hat{I} \\lvert 0 \\rangle = \\hat{X} \\hat{X} \\lvert 0 \\rangle = \\hat{X} \\lvert 1 \\rangle = \\lvert 0 \\rangle $$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises\n",
    "\n",
    "\n",
    "<ol>\n",
    "\n",
    "<li>\n",
    "Given the following wavefunctions describing the state of a system\n",
    "\n",
    "<ol>\n",
    "<li>\n",
    "$ \\lvert \\psi \\left( \\vec{r} , t \\right)\\rangle = \\frac{1}{\\sqrt{2}} e^{i3t}  \\lvert \\psi \\rangle_1 + \\frac{1}{\\sqrt{2}} e^{i3t} \\lvert \\psi \\rangle_2   $\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$ \\lvert \\psi \\left( \\vec{r} , t \\right)\\rangle = \\frac{1}{\\sqrt{8}} e^{i5t}  \\lvert \\psi \\rangle_1 + \\frac{\\sqrt{5}}{\\sqrt{8}} e^{i5t} \\lvert \\psi \\rangle_2 + \\frac{\\sqrt{2}}{\\sqrt{8}} e^{i2t} \\lvert \\psi \\rangle_2   $\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$ \\lvert \\psi \\left( \\vec{r} , t \\right)\\rangle = \\frac{1}{\\sqrt{3}} e^{i2t}  \\lvert \\psi \\rangle_1 + \\frac{\\sqrt{2}}{\\sqrt{3}} e^{i7t} \\lvert \\psi \\rangle_2   $\n",
    "</li>\n",
    "\n",
    "What is the probability that the system is in the state $\\lvert \\psi \\rangle_1$? and the probability of $\\lvert \\psi \\rangle_2$?\n",
    "</ol>\n",
    "\n",
    "\n",
    "</li>\n",
    "\n",
    "\n",
    "<li>\n",
    "Find the expectaion value of the operator $\\hat{X}$ for a qubit in the following states:\n",
    "\n",
    "<ol>\n",
    "<li>\n",
    "$ \\lvert \\psi \\rangle = \\lvert 0 \\rangle$\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$ \\lvert \\psi \\rangle = \\lvert 1 \\rangle$\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$ \\lvert \\psi \\rangle = \\frac{1}{\\sqrt{2}} \\left( \\lvert 0 \\rangle + \\lvert 1 \\rangle \\right)$\n",
    "</li>\n",
    "</ol>\n",
    "\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Given a system in the initial state $\\lvert \\psi \\left( t_0 \\right)\\rangle = \\lvert 0 \\rangle$. Find the state of the system at following times, given the time evolution operators\n",
    "\n",
    "\n",
    "<ol>\n",
    "<li>\n",
    "$ \\hat{U}_1 = \\hat{X}$\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$ \\hat{U}_2 = \\hat{X}$\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$ \\hat{U}_3 = \\hat{X}$\n",
    "</li>\n",
    "</ol>\n",
    "\n",
    "\n",
    "</li>\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "</ol>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
    "\n",
    "[1] P. A. M. Dirac, The Principles of Quantum Mechanics (1947 Clarendon Press, Oxford).\n",
    "\n",
    "[2] J. von Neumann, Mathematische Grundlagen der Quanten-Mechanik, Springer-Verlag, Berlin, 1932."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
